#pragma once
//工具类
#include<string>
#include<sys/socket.h>
class Util
{
    private:
    public:
    static int ReadLine(int fd,std::string&out)
    {
        char ch='x';
        ssize_t n;
        while(ch!='\n')
        {
          n=   recv(fd,&ch,1,0);
         if(n>0)
         {
            //统一处理成'\n'
            if(ch=='\r')
            {
                //窥探下一个,不从tcp接收缓冲区里拿走,但ch已被修改
                recv(fd,&ch,1,MSG_PEEK);
                if(ch=='\n')
                //如果是带走
                recv(fd,&ch,1,0);
                else
                {
                    ch='\n';
                }
            }
            out.push_back(ch);
         }
         else if(n<0)
         {
          lg(INFO,"recv line error!,errstring :%s",strerror(errno));
            return -1;
         }
         else
         {
          lg(INFO,"write close!");
            return 0;
         }
        }
       // lg(DEBUG,"读到'\\n'");
        return 1;
    }
    static bool CutString(const std::string&target,std::string&out1,std::string&out2,std::string sep)
    {
        //1.查找到分隔符
      size_t pos=  target.find(sep);//找到返回下标
      if(pos!=std::string::npos)
      {
        //找到了 ->  k: v
        out1=target.substr(0,pos);//从那个下标开始截取，截取多少字节，返回值为截取后的内容
        out2=target.substr(pos+sep.size());
        return true;
      }
      return false;
        //2.切割字符串获取想要的k、v
    }
};